﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;

using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HB.WpfEx;
using HB.MVVM;
using System.Drawing;
using System.Windows.Markup;
using Microsoft.Win32;
namespace Demo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

           


            ImageScale IS = new ImageScale(hb);
            byte[] data = new byte[390 * 400 * 4];
            Random R = new Random();
            R.NextBytes(data);
            WriteableBitmap wb = new WriteableBitmap(390, 400, 96, 96, System.Windows.Media.PixelFormats.Bgr32, null);
            wb.Lock();
            wb.WritePixels(new Int32Rect(0, 0, 390, 400), data, 390 * 4, 0);
            wb.AddDirtyRect(new Int32Rect(0, 0, 390, 400));
            wb.Unlock();
            ImageGraphics IG = new ImageGraphics(hb, wb);
            VM.Instance.IocRegister(IS);
            VM.Instance.IocRegister(IG);
            VM.Instance.Connect(this);
        }

    }
    public class VM : ViewModelBase<VM>
    {


        ImageScale scale = null;
        ImageGraphics G;
        //  public WriteableBitmap wb { get; set; } = new WriteableBitmap(400, 400, 96, 96, PixelFormats.Bgr24, null);
        System.Timers.Timer timer = new System.Timers.Timer() { Interval = 10 };
        public ObservableCollectionEx<PixelInfo> points { get; set; } = new ObservableCollectionEx<PixelInfo>();
        public BitmapSource testMap { get => Get<BitmapSource>(); set => Set(value); }
        public override void VM_Init(object sender, RoutedEventArgs e)
        {
            IocResolve(out scale);
            IocResolve(out G);
            scale.MouseMove += Scale_MouseMove;


            timer.Elapsed += Timer_Elapsed;
            //  IG2 = new //ImageGraphics(realmap);
        }
        ImageGraphics IG2;
        public string xy { get => GetString(); set => Set(value); }
        Random R = new Random();
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            var start = R.Next(0, 400);
            var end = R.Next(0, 400);
            G.UsingGraphics(g =>
            {
                g.DrawLine(Pens.Red, 0, start, 400, end);
            });
            realmap.UsingGraphics(g =>
            {

                g.DrawLine(Pens.Red, 0, start, 400, end);
            });

        }
        public WriteableBitmap realmap { get; set; } = new WriteableBitmap(400, 400, 96, 96, System.Windows.Media.PixelFormats.Bgr32, null);
        public ICommand cmdrun => GetCommand(() =>
        {
            timer.Enabled = !timer.Enabled;
        });
        public ICommand cmdshowbg => GetCommand(() =>
        {
            G.SetToBgImage(false);

        });
        public ICommand cmdsave => GetCommand(() =>
        {
            var f = $"T:\\{R.Next()}.png";
            G.SaveToPng(f);
        });
        private void Scale_MouseMove(ImagePoint obj)
        {
            xy = $"{obj.X},{obj.Y},index={obj.Index}";
        }

        public ICommand cmdOpenFile => GetCommand(() =>
        {
            ImageHitTest hi = new ImageHitTest(400, 400);
            Random r = new Random();
            hi.UsingGraphics(g =>
            {
                g.Clear(System.Drawing.Color.Black);


                g.DrawLine(g.GetAlphaPen(Color.Red, 128), 0, r.Next(0, 400), 400, r.Next(0, 400));
                g.FillRectangle(g.GetAlphaBrush(Color.Blue, 128), new System.Drawing.Rectangle(100, 100, 200, 200));
            });
            var ls = hi.GetHitPixel(p => p.R > 0 && p.B > 0);
            points.Clear();
            points.AddRange(ls.ToArray());
            testMap = hi.MapView;
            hi.Dispose();
        });
    }
}
